package com.cskaoyan.util;

import com.cskaoyan.demo7.mapper.RegionMapper;
import jdk.dynalink.beans.StaticClass;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Statement;

public class MyBatisUtils {
    static SqlSessionFactory sqlSessionFactory;
    static RegionMapper regionMapper;
    static SqlSession sqlSession;


    public static void init() throws IOException {
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        sqlSession = sqlSessionFactory.openSession();
        regionMapper = sqlSession.getMapper(RegionMapper.class);
    }

    public static RegionMapper getRegionMapper() throws IOException {
        init();
        return regionMapper;
    }

    public void release(){
        if (sqlSession != null){
            sqlSession.commit();
            sqlSession.close();
        }
    }
}
